package manager.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import manager.bean.LayData;
import manager.bean.User;
import manager.dao.UserDao;
import manager.util.JDBC;
import manager.util.MD5;
import manager.util.SYS;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet("/user/*")
public class UserServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*根据不同的二级路径  处理不同的方法  --增删改查*/
        Object path = req.getAttribute(SYS.REQUEST_PATH);
        if ("list".equals(path)){
            //查询
            List list = list(req);
            int count = count(req);
            LayData layData = new LayData(count,list);
            //用字符串来表示一个对象  JSON格式
            String json = JSON.toJSONString(layData, SerializerFeature.WriteDateUseDateFormat);
            resp.getWriter().write(json);
            System.out.println(json);
        }else if ("add".equals(path)){
            //新增
            int count = add(req);
            resp.getWriter().write(""+count);
        }else if ("edit".equals(path)){
            //修改
            int count = edit(req);
            resp.getWriter().write(""+count);
        }else if ("editpass".equals(path)){
            //修改
            int count = editpass(req);
            resp.getWriter().write(""+count);
        }else if ("delete".equals(path)){
            //删除
            int count = delete(req);
            resp.getWriter().write(""+count);
        }else if ("check".equals(path)){
            int res = check(req,resp);
//            System.out.println(res);
            resp.getWriter().write(String.valueOf(res));
        }
        else {
            //无效的
        }
    }
    private UserDao dao = new UserDao();
    private List list(HttpServletRequest request){
        List list = null;
        String checktext = request.getParameter("checktext");
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        int start = (page - 1) * limit;
        String sql = "select * from user ";
        if (checktext!=null){
            sql += " where username like ?  limit ?,?";
            System.out.println(sql+"***");
            list = dao.getUser(sql,"%"+checktext+"%",start,limit);
        }else {
            sql += "limit ?,? ";
            System.out.println(sql);
            list = dao.getUser(sql,start,limit);
        }
        return list;
    }

    private int count(HttpServletRequest request){
        String checktext = request.getParameter("checktext");
        int count = 0;
        String sql = "select count(*) from user";
        if (checktext!=null){
            sql += "where username like ? ";
            count = dao.countUser(sql,"%"+checktext+"%");
        }else {
            count = dao.countUser(sql);
        }
        return count;

    }

    private int delete (HttpServletRequest request){
        String id = request.getParameter("id");
        String sql = "delete from user where id = ?";
        int count = dao.deleteUser(sql,id);
        return count;

    }

    private int add(HttpServletRequest request){
        String username = request.getParameter("username");
        String staff_id = request.getParameter("staff_id");
        String sql = "insert into user (staff_id,username,userpass) values(?,?,?)";
        System.out.println(sql);
        System.out.println(username);
        System.out.println(staff_id);
        int count = dao.addUser(sql,staff_id,username,"86e22f209dd4b1e8a8a3d9a200860fae4F");
        return count;
    }
    private int edit(HttpServletRequest request){
        String username = request.getParameter("username");
        String id = request.getParameter("id");
        String sql = "update user set username=? where id = ?";
        System.out.println(sql);
        int count = dao.editUser(sql,username,id);
        return count;
    }
    private int editpass(HttpServletRequest request){
        String userpass = request.getParameter("userpass");
        String username = request.getParameter("username");
        userpass = MD5.hexpass(userpass);
        String sql = "update user set userpass = ? where username = ? ";
        System.out.println(sql);
        System.out.println(username);
        System.out.println(userpass);
        int count = dao.editUser(sql,userpass,username);
//        System.out.println(count+"-----");
        return count;
    }
    private int check(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String username = req.getParameter("username");
        String userpass = req.getParameter("userpass");
        HttpSession session = req.getSession();
        String sql = "select userpass,staff_id from user where username = '" + username + "'";
        List<User> list = dao.getUser(sql);
        System.out.println(list);

        if (list.size()!=0) {
            User d_user = list.get(0);
            if (MD5.compare_pass(userpass,d_user.getUserpass())) {
                int staff = d_user.getStaff_id();
//                System.out.println(staff);
                String sql1 = "select userstate from staff where id = "+staff;
                int res = dao.state(sql1);
//                System.out.println(res+"------------------");
                if (res==0){
//                    System.out.println("00000000000000000000000000000");
//                    resp.getWriter().write("change_password");
                    return 0;
                }else if (res==1){
                    System.out.println("1111111111111111111111111");
                    session.setAttribute("login_user",d_user);
                    Date d = new Date();
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String time = sdf.format(d);
                    System.out.println(time);
                    String sqltime = "update user set lastlogin = ? where username = ?";
                    dao.editUser(sqltime,time,username);
//                    resp.getWriter().write("success");
                    return 1;
                }else if (res==2){
                    System.out.println("2222222222222222222");
//                    resp.getWriter().write("cant login");
                    return 2;
                }
            }else {
                System.out.println("fffffffffffffffff");
//                resp.getWriter().write("failure");
                return 3;
            }
        }
        else return 4;

        return 5;
    }
}
